package cn.yunyichina.provider.clinic.thread;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;

import cn.yunyichina.provider.clinic.dao.MzFeeDao;
import cn.yunyichina.provider.clinic.dao.MzFeeDetailDao;
import cn.yunyichina.provider.clinic.entity.MzFee;
import cn.yunyichina.provider.clinic.entity.MzFeeDetail;
import cn.yunyichina.provider.clinic.manager.ClinicBizManager;
import cn.yunyichina.provider.clinic.vo.ListMzfeeVoResponse;
import cn.yunyichina.provider.framework.common.PKGenerator;
import cn.yunyichina.provider.framework.data.SpringContextHandler;

/**
 * @Package: com.yy.easyhealth.biz.datastorage.thread
 * @ClassName: SaveDataMZFeeRunnable
 * @Statement:
 *             <p>
 *             门诊待缴费记录入库
 *             </p>
 * @JDK version used: 1.6
 * @Author: dfw
 * @Create Date: 2015-8-10
 * @modify By:
 * @modify Date:
 * @Why&What is modify:
 * @Version: 1.0
 */
public class SaveDataMZFeeRunnable implements Runnable {

    private Logger logger = LoggerFactory.getLogger(SaveDataMZFeeRunnable.class);

    private List<MzFee> mzFees;

    private List<ListMzfeeVoResponse> data;

    public SaveDataMZFeeRunnable() {
        super();
    }

    public SaveDataMZFeeRunnable(List<ListMzfeeVoResponse> data) {
        this.data = data;
    }

    @Override
    public void run() {
        saveDataEx();
    }

    @SuppressWarnings("unchecked")
    public void saveDataEx() {
        MzFeeDao mzFeeDao = SpringContextHandler.getBean(MzFeeDao.class);
        MzFeeDetailDao mzFeeDetailDao = SpringContextHandler.getBean(MzFeeDetailDao.class);
        ClinicBizManager clinicBizManager = SpringContextHandler.getBean(ClinicBizManager.class);

        List<String> mzFeeIds = new ArrayList<String>();
        for (ListMzfeeVoResponse resp : data) {
            mzFeeIds.add(resp.getMzFeeId());
        }
        Long startTime = System.currentTimeMillis();
        List<MzFee> dataMZFees = new ArrayList<MzFee>();
        // DataMZFee 和 MZFee
        List<MzFeeDetail> dataMZFeeDetails = new ArrayList<MzFeeDetail>();
        for (int i = 0; i < data.size(); i++) {
            ListMzfeeVoResponse resp = data.get(i);

            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("hospitalCode", resp.getHospitalCode());
            paramMap.put("branchHospitalCode", resp.getBranchCode());
            paramMap.put("mzFeeIds", mzFeeIds);
            List<String> existsMzFeeIds = mzFeeDao.findByBranchHospitalCodeAndMzFeeIds(paramMap);

            if (!existsMzFeeIds.contains(resp.getMzFeeId())) {
                if (logger.isDebugEnabled()) {
                    logger.debug("分院[{}]不存在数据[{}]", resp.getBranchCode(), resp.getMzFeeId());
                }
                MzFee mzFee = new MzFee();
                mzFee.setBranchCode(resp.getBranchCode());
                mzFee.setCardNo(resp.getPatCardNo());
                mzFee.setCardType(resp.getPatCardType());
                mzFee.setClinicTime(resp.getCreatedTime());
                mzFee.setDeptName(resp.getDeptName());
                mzFee.setDoctorName(resp.getDoctorName());
                mzFee.setHospitalCode(resp.getHospitalCode());
                mzFee.setId(PKGenerator.generateId());
                mzFee.setMzFeeId(resp.getMzFeeId());
                mzFee.setPayAmout(resp.getPayAmount() != null ? String.valueOf(resp.getPayAmount()) : "");
                mzFee.setPayType(resp.getPayType() != null ? String.valueOf(resp.getPayType()) : "");
                mzFee.setStorageTime(System.currentTimeMillis());
                mzFee.setTotalAmout(resp.getTotalAmount() != null ? String.valueOf(resp.getTotalAmount()) : "");
                mzFee.setHospitalName(resp.getHospitalName());
                mzFee.setBranchName(resp.getBranchName());

                // List<MZFeeDetail> mzFeeDetails =
                // clinicBizManager.getMZFeeDetail(paramsMap);
                // if (logger.isDebugEnabled()) {
                // logger.debug("mzFeeDetails: {}",
                // JSON.toJSONString(mzFeeDetails));
                // }
                // if (!CollectionUtils.isEmpty(mzFeeDetails)) {
                // for (MZFeeDetail mzFeeDetail : mzFeeDetails) {
                // MzFeeDetail dataMzFeeDetail = new MzFeeDetail();
                // try {
                // BeanUtils.copyProperties(dataMzFeeDetail, mzFeeDetail);
                // dataMzFeeDetail.setMzFeeId(dataMzFee.getMzFeeId());
                //
                // dataMZFeeDetails.add(dataMzFeeDetail);
                // } catch (IllegalAccessException e) {
                // e.printStackTrace();
                // } catch (InvocationTargetException e) {
                // e.printStackTrace();
                // }
                // }
                // }
                dataMZFees.add(mzFee);
            } else {
                if (logger.isDebugEnabled()) {
                    logger.debug("分院[{}]存在相同数据[{}]", resp.getBranchCode(), resp.getMzFeeId());
                }
            }

            if (dataMZFees.size() > 0) {
                if (!CollectionUtils.isEmpty(dataMZFees)) {
                    mzFeeDao.batchInsert(dataMZFees);
                }
                if (!CollectionUtils.isEmpty(dataMZFeeDetails)) {
                    mzFeeDetailDao.batchInsert(dataMZFeeDetails);
                }
            }
            logger.info("---------------------------------------门诊代缴费数据入库, 耗时："
                    + (System.currentTimeMillis() - startTime) + "ms --------------------------------------");
        }
    }
}
